<?php

namespace App\Http\Model;

use Illuminate\Database\Eloquent\Model;

class Category extends Model
{
    protected $table = 'blog_category';
    protected $primaryKey = 'cate_id';
    public $timestamps = false;
    protected $guarded = [];

    public function tree()
    {
        $data = $this::all();
        return $this->getTree($data, 0, 0);

    }

    public function getTree($data, $pid=0, $lev=0)
    {
        $arr = array();
        foreach($data as $k=>$v){
            if($v['cate_pid']==$pid) {
                $v['lev'] = $lev;
                $arr[] = $v;
                $arr = array_merge($arr, $this->getTree($data, $v['cate_id'], $lev+1));
            }
        }
        return $arr;
    }
}
